﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Crosland.Automotriz.Logic.Linq;
using Crosland.Automotriz.Seguridad.ControlExeption;

namespace Crosland.Automotriz.Web.Repuestos
{
    public partial class wFrmCreditoClienteGarantia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false) {

                this.ListarCreditoClienteGarantia();
            
            }
        }



        private void ListarCreditoClienteGarantia() {

            try
            {
                clsT_Cre_Cliente_GarantiaLogic clst_cre_cliente_garantialogic = new clsT_Cre_Cliente_GarantiaLogic();
                grdCreditoClienteGarantia.DataSource = clst_cre_cliente_garantialogic.lista();
                grdCreditoClienteGarantia.DataBind();
            }
            catch (Automotriz.Logic.ControlExeption.DataException ex)
            {
                GuardarExceptionLog(ex.Message, ex, "ListarCreditoClienteGarantia");
                MensajesError(ex.Message, ex, "ListarCreditoClienteGarantia");
            }
            catch (Automotriz.Logic.ControlExeption.RunException ex)
            {
                GuardarExceptionLog(ex.Message, ex, "ListarCreditoClienteGarantia");
                MensajesError(ex.Message, ex, "ListarCreditoClienteGarantia");
            }
            catch (Exception ex) {
                GuardarExceptionLog("Error en la aplicación", ex, "ListarCreditoClienteGarantia");
                MensajesError("Error en la aplicación", ex, "ListarCreditoClienteGarantia");            
            }
      
        }



        private void GuardarExceptionLog(String msgUsu, Exception ex, String metError)
        {
            try
            {
                String msgReal, fhError, pagError, codUsu;
                msgReal = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
                pagError = new System.IO.FileInfo(Page.Request.Url.AbsolutePath).Name;
                codUsu = Session["User"].ToString();
                fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");

                System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
                int number = 0;
                for (int i = 0; i < trace.FrameCount; i++)
                {
                    System.Diagnostics.StackFrame sf = trace.GetFrame(i);
                    number = sf.GetFileLineNumber();
                }
                fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
                new Logg().ErrorLog(Server.MapPath("~/Logs/ErrorLog.csv"), msgUsu, msgReal, pagError, metError, codUsu, number);
            }
            catch (DataException e) { MensajesError(e.Message, e, "GuardarExceptionLog"); }
            catch (RunException re) { MensajesError(re.Message, re, "GuardarExceptionLog"); }


        }


        private void MensajesError(String msgUsu, Exception ex, String metError)
        {
            Crosland.Automotriz.Web.UserControl.MensajeError uc = (Crosland.Automotriz.Web.UserControl.MensajeError)Master.FindControl("ucErrorMensaje");
            String msgReal, fhError, pagError, codUsu;
            msgReal = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
            pagError = new System.IO.FileInfo(Page.Request.Url.AbsolutePath).Name;
            codUsu = Session["User"].ToString();
            fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
            System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
            int number = 0;
            for (int i = 0; i < trace.FrameCount; i++)
            {
                System.Diagnostics.StackFrame sf = trace.GetFrame(i);
                number = sf.GetFileLineNumber();
            }
            uc.Errores(msgUsu, msgReal, fhError, pagError, metError, codUsu, number);
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            Session["operacion"] = "N";
            Response.Redirect("~/Vehiculos/wFrmCreditoClienteGarantiaActualizar.aspx?id=3");
        }

        protected void btnActualizar_Click(object sender, EventArgs e)
        {

           int index= grdCreditoClienteGarantia.FocusedRowIndex;

           if (index == -1) {
               lblMensaje.Text = "Por Favor seleccione un registro ";
               popupErrores.ShowOnPageLoad = true;
               return;
           }
           Session["operacion"] = "M";
           Session["ID_CRE_CLIENTE_GARANTIA"] = grdCreditoClienteGarantia.GetRowValues(index, "ID_CRE_CLIENTE_GARANTIA");
           Response.Redirect("~/Vehiculos/wFrmCreditoClienteGarantiaActualizar.aspx?id=3");
        }


        protected void btnSalir_Click(object sender, EventArgs e)
        {
            Session["ID_CRE_CLIENTE_GARANTIA"] = null;
            Response.Redirect("~/Repuestos/wFrmDefault.aspx?id=3");
        }

        protected void grdCreditoClienteGarantia_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            this.ListarCreditoClienteGarantia();
        }

        protected void grdCreditoClienteGarantia_PageIndexChanged(object sender, EventArgs e)
        {
            this.ListarCreditoClienteGarantia();
        }

    }
}